package es.deusto.tralala.db;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

import javax.jdo.Extent;
import javax.jdo.JDOHelper;
import javax.jdo.ObjectState;
import javax.jdo.PersistenceManager;
import javax.jdo.PersistenceManagerFactory;
import javax.jdo.Query;
import javax.jdo.Transaction;

import es.deusto.tralala.data.Cancion;
import es.deusto.tralala.data.Reproduccion;
import es.deusto.tralala.data.Usuario;
import es.deusto.tralala.data.dto.CancionDTO;

public class JDO implements IDAO {
	
	private PersistenceManagerFactory pmf;
	
	public JDO() {
		pmf = JDOHelper.getPersistenceManagerFactory("tralalaJDO");
	}
	
	@Override
	public void addUsuario(Usuario usuario) {
		PersistenceManager pm = pmf.getPersistenceManager();
		Transaction tx = pm.currentTransaction();
		
		try
		{
		    tx.begin();
		    
		    pm.makePersistent(usuario);
		    
		    tx.commit();
		}
		finally
		{
		    if (tx.isActive())
		    {
		        tx.rollback();
		       
		    }
		    pm.close();
		}
	
	}
	
	@Override
	public Usuario getUsuario(String nick) {
		Usuario usuario;
		PersistenceManager pm = pmf.getPersistenceManager();
		Transaction tx = pm.currentTransaction();
		
		try {
			tx.begin();
			Extent<Usuario> e = pm.getExtent(Usuario.class);
			Query q = pm.newQuery(e, "nick == " + "\"" + nick + "\"");
			q.setUnique(true);
			usuario = (Usuario) q.execute();

			tx.commit();
		}
		catch (NullPointerException e)
		{
			usuario = null;
		}
		finally {
			if (tx.isActive()) {
				tx.rollback();
			}
			pm.close();
		}
		
		return usuario;
	}

	@Override
	public void setUsuario(Usuario usuario) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void addCancion(Cancion cancion) {
		PersistenceManager pm = pmf.getPersistenceManager();
		Transaction tx = pm.currentTransaction();
		
		try
		{
		    tx.begin();
		    
		    pm.makePersistent(cancion);
		    
		    tx.commit();
		}
		finally
		{
		    if (tx.isActive())
		    {
		        tx.rollback();
		       
		    }
		    pm.close();
		}
	
	}
	
	@Override
	public List<Cancion> getCanciones() {
		// Perform some query operations
		PersistenceManager pm = pmf.getPersistenceManager();
		Transaction tx = pm.currentTransaction();
		List<Cancion> canciones = new ArrayList<>();
		
		try {
			tx.begin();
			Extent<Cancion> e = pm.getExtent(Cancion.class);
            Iterator<Cancion> iter = e.iterator();

			while (iter.hasNext())
			{
				canciones.add(iter.next());
			}

			tx.commit();
		}
		catch (NullPointerException e)
		{
			canciones = null;
		}
		finally {
			if (tx.isActive()) {
				tx.rollback();
			}
			pm.close();
		}
		
		return canciones;
	}
	
	@Override
	public Cancion getCancion(String titulo) {
		Cancion cancion;
		PersistenceManager pm = pmf.getPersistenceManager();
		Transaction tx = pm.currentTransaction();
		
		try {
			tx.begin();
			Extent<Cancion> e = pm.getExtent(Cancion.class);
			Query q = pm.newQuery(e, "titulo == " + "\"" + titulo + "\"");
			q.setUnique(true);
			cancion = (Cancion) q.execute();
			//ObjectState state = JDOHelper.getObjectState(cancion);
		    //System.out.println(state);
			
			tx.commit();
		}
		catch (NullPointerException e)
		{
			cancion = null;
		}
		finally {
			if (tx.isActive()) {
				tx.rollback();
			}
			pm.close();
		}
		
		return cancion;
	}
	
	@Override
	public List<Cancion> searchCancion(String titulo) {
		List<Cancion> canciones;
		PersistenceManager pm = pmf.getPersistenceManager();
		Transaction tx = pm.currentTransaction();
		
		try {
			tx.begin();
			Extent<Cancion> e = pm.getExtent(Cancion.class);
			Query q = pm.newQuery(e, "titulo == " + "\"" + titulo + "\"");
			canciones = (List<Cancion>) q.execute();
			//ObjectState state = JDOHelper.getObjectState(cancion);
		    //System.out.println(state);
			
			tx.commit();
		}
		catch (NullPointerException e)
		{
			canciones = null;
		}
		finally {
			if (tx.isActive()) {
				tx.rollback();
			}
			pm.close();
		}
		
		return canciones;
	}

	@Override
	public void setCancion(Cancion cancion) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void addReproduccion(Reproduccion reproduccion) {
		PersistenceManager pm = pmf.getPersistenceManager();
		Transaction tx = pm.currentTransaction();
		
		try
		{
		    tx.begin();
		    
		    //ObjectState state = JDOHelper.getObjectState(reproduccion.getCancion());
		    //System.out.println(state);
		    
		    pm.makePersistent(reproduccion);
		    
		    tx.commit();
		}
		finally
		{
		    if (tx.isActive())
		    {
		        tx.rollback();
		       
		    }
		    pm.close();
		}
	
	}	

	@Override
	public void setReproduccion(Reproduccion reproduccion) {
		// TODO Auto-generated method stub
		
	}
}
